package com.shimao.celnet.payment.controller.api;

import com.shimao.celnet.payment.model.BEstate;
import com.shimao.celnet.payment.model.BInvoiceCompany;
import com.shimao.celnet.payment.model.PPayBusiness;
import com.shimao.celnet.payment.model.PPayCust;
import com.shimao.celnet.payment.model.request.CallIn_OutBusinessInformationReqIn;
import com.shimao.celnet.payment.model.response.ErrorInfoEnum;
import com.shimao.celnet.payment.model.response.ResponseJson;
import com.shimao.celnet.payment.service.BEstateService;
import com.shimao.celnet.payment.service.BInvoiceCompanyService;
import com.shimao.celnet.payment.service.PPayBusinessService;
import com.shimao.celnet.payment.service.PPayCustService;
import com.shimao.celnet.payment.util.FinalUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by XSW on 2018/10/24.
 */
@RestController
@RequestMapping(value = "/payment")
@Api("接口：pay-150(信息)-外部商家信息申请<outBusinessInformationReq> 外部系统 → 支付系统，数据：开票和支付相关销方信息的")
public class CalIn_outBusinessInformationReqInContrller {

    protected Logger logger = Logger.getLogger(this.getClass());

    @Autowired
    private BInvoiceCompanyService bInvoiceCompanyService;

    @Autowired
    private PPayBusinessService pPayBusinessService;

    @Autowired
    private BEstateService bEstateService;

    @Autowired
    private PPayCustService pPayCustService;


    @ApiOperation("获取外部请求数据")
    @RequestMapping(value = "/busInforReq", method = RequestMethod.POST)
    public String OutBusinessInformationReqIn(@RequestBody @Valid CallIn_OutBusinessInformationReqIn requestJson, BindingResult result){
        ResponseJson retJson = new ResponseJson(ErrorInfoEnum.SUCCESS, "");
        logger.info("pay-150-外部商家信息申请<outBusinessInformationReq> 请求JSON:" + requestJson.toString());
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        //通过valid验证
        if (result.hasErrors()) {
            for (FieldError error : result.getFieldErrors()) {
                logger.debug("缺少必要参数：" + error.getDefaultMessage());
                retJson = new ResponseJson(ErrorInfoEnum.NOT_PARAMS, error.getDefaultMessage());
                return retJson.toString();
            }
        }
        String appKey = request.getHeader(FinalUtil.HEADER_APP_KEY);//获取header中的app_key
        String interfaceId = request.getHeader(FinalUtil.HEADER_INTERFACE_ID);//获取header中的interface_id
        logger.info("应用编号：" + appKey);
        logger.info("接口编号：" + interfaceId);
        //创建小区对象
      //  BEstate estate = new BEstate();
       // String estateGuid = bEstateService.getGuid();

        //创建商家支付信息
        PPayBusiness business = new PPayBusiness();
        String businessGuid = pPayBusinessService.getGuid();
        business.setGuid(businessGuid);
        business.setAppKey(appKey);
        business.setInterfaceId(interfaceId);
        business.setEnterpriseCode(requestJson.getEnterpriseCode());
        business.setEnterpriseName(requestJson.getEnterpriseName());
        business.setEnterpriseAddress(requestJson.getEnterpriseAddress());
        business.setRegisteredAddress(requestJson.getRegisteredAddress());
        business.setContactorType(requestJson.getEnterprisePhone());
        business.setContactorName(requestJson.getContactorName());
        business.setContactorEmail(requestJson.getContactorEmail());
        business.setCompanySfguid(requestJson.getBusinessId());
        business.setCreatedUserGuid(FinalUtil.INTER_FACE_USER_ID);
        business.setUpdateUserGuid(FinalUtil.INTER_FACE_USER_ID);
        pPayBusinessService.save(business);
        //创建销方信息对象
        BInvoiceCompany bInvoiceCompany = new BInvoiceCompany();
        bInvoiceCompany.setGuid(bInvoiceCompanyService.getGuid());
        bInvoiceCompany.setSellerTaxNo(requestJson.getSellerTaxNo());
        bInvoiceCompany.setCompanyName(requestJson.getEnterpriseName());
        bInvoiceCompany.setAddressPhone(requestJson.getEnterpriseAddress()+requestJson.getEnterprisePhone());
        bInvoiceCompany.setAddressAccount(requestJson.getOpeningBank()+requestJson.getAccountNo());
        bInvoiceCompany.setEstateSfGuid(requestJson.getBusinessId());
        bInvoiceCompany.setEstateGuid(businessGuid);
        bInvoiceCompany.setCreatedUserGuid(FinalUtil.INTER_FACE_USER_ID);
        bInvoiceCompany.setUpdateUserGuid(FinalUtil.INTER_FACE_USER_ID);
        //保存
        bInvoiceCompanyService.saveByObject(bInvoiceCompany);
        //创建支付商户对象
        if(requestJson.getAlipayAppId() != null && requestJson.getAlipayAppId() != ""){
            PPayCust pPayCust = new PPayCust();
            pPayCust.setEstateGuid(pPayCustService.getGuid());
            pPayCust.setAppId(requestJson.getAlipayAppId());
            pPayCust.setAppKey(requestJson.getAlipayAppKey());
            pPayCust.setCustId(requestJson.getAlipayCustId());
            pPayCust.setPaytype(FinalUtil.ALIPAY_PAY_TYPE);
            pPayCust.setVersion(FinalUtil.PAY_VERSION);
            pPayCust.setEstateGuid(businessGuid);
            pPayCust.setCreatedUserGuid(FinalUtil.INTER_FACE_USER_ID);
            pPayCust.setUpdateUserGuid(FinalUtil.INTER_FACE_USER_ID);
            pPayCustService.saveObject(pPayCust);
        }
        if (requestJson.getWeChatAppId() != null && requestJson.getWeChatAppId() != ""){
            PPayCust pPayCust = new PPayCust();
            pPayCust.setEstateGuid(pPayCustService.getGuid());
            pPayCust.setAppId(requestJson.getWeChatAppId());
            pPayCust.setAppKey(requestJson.getWeChatAppKey());
            pPayCust.setCustId(requestJson.getWeChatCustId());
            pPayCust.setPaytype(FinalUtil.WEChAT_PAY_TYPE);
            pPayCust.setVersion(FinalUtil.PAY_VERSION);
            pPayCust.setEstateGuid(businessGuid);
            pPayCust.setCreatedUserGuid(FinalUtil.INTER_FACE_USER_ID);
            pPayCust.setUpdateUserGuid(FinalUtil.INTER_FACE_USER_ID);
            pPayCustService.saveObject(pPayCust);
        }
        if(requestJson.getCUPAppId() != null && requestJson.getCUPAppId() != ""){
            PPayCust pPayCust = new PPayCust();
            pPayCust.setEstateGuid(pPayCustService.getGuid());
            pPayCust.setAppId(requestJson.getCUPAppId());
            pPayCust.setAppKey(requestJson.getCUPAppKey());
            pPayCust.setCustId(requestJson.getCUPCustId());
            pPayCust.setPaytype(FinalUtil.CUP_PAY_TYPE);
            pPayCust.setVersion(FinalUtil.PAY_VERSION);
            pPayCust.setEstateGuid(businessGuid);
            pPayCust.setCreatedUserGuid(FinalUtil.INTER_FACE_USER_ID);
            pPayCust.setUpdateUserGuid(FinalUtil.INTER_FACE_USER_ID);
            pPayCustService.saveObject(pPayCust);
        }
        return retJson.toString();
    }
}
